package views;

import bean.Cls;
import service.ClassService;
import utils.TableMode;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class ClassList extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    Cls cls2=new Cls();

    public  void setCls(Cls cls) {
        cls2=cls;

    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ClassList frame = new ClassList();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ClassList() {
        setTitle("班级列表");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 731, 594);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(10, 10, 188, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("查询");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Cls cls=new Cls();
                cls.setC_name(textField.getText());

                //根据名字查询班级列表
                ClassService s=new ClassService();
                List<Cls> aList=s.selectClsByName(cls);
                Vector data =new Vector();
                Iterator<Cls> iterator=aList.iterator();
                while(iterator.hasNext()){
                    Cls next = iterator.next();
                    Vector row=new Vector();
                    row.add(next.getC_id());
                    row.add(next.getC_name());
                    data.add(row);
                }

                Vector colNames=new Vector();
                colNames.add("班级ID");
                colNames.add("班级名称");
                TableMode mode=new TableMode(data, colNames);
                table.setModel(mode);
            }
        });
        btnNewButton.setBounds(208, 9, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("设置班级");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Cls cls=new Cls();
                cls.setC_id(table.getValueAt(table.getSelectedRow(), 0).toString());
                cls.setC_name(table.getValueAt(table.getSelectedRow(), 1).toString());
                Main main=new Main();
                main.setCls(cls);
                main.setVisible(true);
                ClassList.this.dispose();
            }

        });
        btnNewButton_1.setBounds(308, 9, 93, 23);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_1_1 = new JButton("导出成绩");
        btnNewButton_1_1.setBounds(409, 9, 93, 23);
        contentPane.add(btnNewButton_1_1);

        JButton btnNewButton_1_2 = new JButton("返回");
        btnNewButton_1_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();
                m.setCls(cls2);
                m.setVisible(true);
                ClassList.this.dispose();
            }
        });
        btnNewButton_1_2.setBounds(509, 9, 93, 23);
        contentPane.add(btnNewButton_1_2);

        JButton btnNewButton_1_3 = new JButton("删除");
        btnNewButton_1_3.setBounds(607, 9, 93, 23);
        contentPane.add(btnNewButton_1_3);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 41, 689, 495);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        //查询班级列表
        ClassService s=new ClassService();
        List<Cls> aList=s.selectCls();
        Vector data =new Vector();
        Iterator<Cls> iterator=aList.iterator();
        while(iterator.hasNext()){
            Cls next = iterator.next();
            Vector row=new Vector();
            row.add(next.getC_id());
            row.add(next.getC_name());
            data.add(row);
        }

        Vector colNames=new Vector();
        colNames.add("班级ID");
        colNames.add("班级名称");
        TableMode mode=new TableMode(data, colNames);
        table.setModel(mode);
    }
}
